//
//  UIButtonExtensions.swift
//  GNAppKit
//
//  Created by Wenkai Fan 范文凯 on 2019/1/8.
//  Copyright © 2019年 Wenkai Fan 范文凯. All rights reserved.
//

import UIKit

// MARK: - Properties
public extension UIButton {
    
    /// GNFoundation: Image of disabled state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_imageForDisabled: UIImage? {
        get {
            return image(for: .disabled)
        }
        set {
            setImage(newValue, for: .disabled)
        }
    }
    
    /// GNFoundation: Image of highlighted state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_imageForHighlighted: UIImage? {
        get {
            return image(for: .highlighted)
        }
        set {
            setImage(newValue, for: .highlighted)
        }
    }
    
    /// GNFoundation: Image of normal state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_imageForNormal: UIImage? {
        get {
            return image(for: .normal)
        }
        set {
            setImage(newValue, for: .normal)
        }
    }
    
    /// GNFoundation: Image of selected state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_imageForSelected: UIImage? {
        get {
            return image(for: .selected)
        }
        set {
            setImage(newValue, for: .selected)
        }
    }
    
    /// GNFoundation: Title color of disabled state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleColorForDisabled: UIColor? {
        get {
            return titleColor(for: .disabled)
        }
        set {
            setTitleColor(newValue, for: .disabled)
        }
    }
    
    /// GNFoundation: Title color of highlighted state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleColorForHighlighted: UIColor? {
        get {
            return titleColor(for: .highlighted)
        }
        set {
            setTitleColor(newValue, for: .highlighted)
        }
    }
    
    /// GNFoundation: Title color of normal state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleColorForNormal: UIColor? {
        get {
            return titleColor(for: .normal)
        }
        set {
            setTitleColor(newValue, for: .normal)
        }
    }
    
    /// GNFoundation: Title color of selected state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleColorForSelected: UIColor? {
        get {
            return titleColor(for: .selected)
        }
        set {
            setTitleColor(newValue, for: .selected)
        }
    }
    
    /// GNFoundation: Title of disabled state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleForDisabled: String? {
        get {
            return title(for: .disabled)
        }
        set {
            setTitle(newValue, for: .disabled)
        }
    }
    
    /// GNFoundation: Title of highlighted state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleForHighlighted: String? {
        get {
            return title(for: .highlighted)
        }
        set {
            setTitle(newValue, for: .highlighted)
        }
    }
    
    /// GNFoundation: Title of normal state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleForNormal: String? {
        get {
            return title(for: .normal)
        }
        set {
            setTitle(newValue, for: .normal)
        }
    }
    
    /// GNFoundation: Title of selected state for button; also inspectable from Storyboard.
    @IBInspectable public var gn_titleForSelected: String? {
        get {
            return title(for: .selected)
        }
        set {
            setTitle(newValue, for: .selected)
        }
    }
    
}

// MARK: - Methods
public extension UIButton {
    
    private var gn_states: [UIControl.State] {
        return [.normal, .selected, .highlighted, .disabled]
    }
    
    /// GNFoundation: Set image for all states.
    ///
    /// - Parameter image: UIImage.
    public func gn_setImageForAllStates(_ image: UIImage) {
        gn_states.forEach { self.setImage(image, for: $0) }
    }
    
    /// GNFoundation: Set title color for all states.
    ///
    /// - Parameter color: UIColor.
    public func gn_setTitleColorForAllStates(_ color: UIColor) {
        gn_states.forEach { self.setTitleColor(color, for: $0) }
    }
    
    /// GNFoundation: Set title for all states.
    ///
    /// - Parameter title: title string.
    public func gn_setTitleForAllStates(_ title: String) {
        gn_states.forEach { self.setTitle(title, for: $0) }
    }
    
    /// GNFoundation: Center align title text and image on UIButton
    ///
    /// - Parameter spacing: spacing between UIButton title text and UIButton Image.
    public func gn_centerTextAndImage(spacing: CGFloat) {
        let insetAmount = spacing / 2
        imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
        contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
    }
    
}


